<template>
  <div class='review-dialog'>
    <el-dialog
      title="分配订单"
      :visible.sync="isShow"
      width="40%"
      center
      :before-close="handleClose">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" label-position="top">
        <el-form-item label="徒弟" prop="student_id">
          <el-select v-model="form.student_id" placeholder="请选择徒弟">
            <el-option
              v-for="item in studentData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isShow: false,
      form: {
        id: null,
        student_id: ''
      },
      rules: {
        student_id: [
          { required: true,message: '请选择徒弟', trigger: 'change' }
        ]
      },
      studentData: []
    }
  },
  methods: {
    show (data) {
      this.isShow = true
      this.form.id = data.id
      this.getStudent()
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitPost()
          this.isShow = false
        } else {
          return false
        }
      });
    },
    submitPost () {
      let params = {
        student_id: this.form.student_id,
      }

      this.$axios.put(`api/order/${this.form.id}/teacher_distribution_order`, params).then(res => {
        if (res.data.code == 200) {
          this.$alert('<span>订单分配成功，请在<i class="font_danger">25</i>天内完成订单，如案件撰写超时，案件结算时将按<i class="font_danger">20</i>元/天扣费！</span>', '提示', {
            confirmButtonText: '确定',
            center: true,
            dangerouslyUseHTMLString: true,
            callback: action => {
            }
          })
          this.$emit('refresh')
          this.handleClose()
        } else if (res.data.code == 400) {
          // this.$message.error(res.data.message)
          this.$alert(res.data.message, '提示', {
            confirmButtonText: '确定',
            center: true,
            callback: action => {
            }
          })
        }
      }).catch(err => {

      })
    },
    handleClose () {
      if (this.type == 1) {
        this.$emit('refresh')
      }
      this.$refs['ruleForm'].resetFields()
      this.isShow = false
    },
    getStudent() {
      this.$axios.get('api/user/get_student_info').then( res => {
        if (res.data.code == 200) {
          this.studentData = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
}
</script>

<style lang='less' type='text/css'>
  .review-dialog {
    .el-select {
      width: 100%;
    }
  }
</style>
